<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
    function handleTime(dateStr) {
        // console.log(dateStr);
        if (dateStr === undefined) {
            return;
        }
        var date = new Date(dateStr);
        // console.log(date);
        
        var now = new Date();
        var digit = parseInt((now.getTime() - date.getTime()) / 1000);
        console.log(digit);
        
        var suffix = '秒前';

        if (digit < 60) {
            return '刚刚';
        } else {
            digit = parseInt(digit / 60);
            // console.log(digit);
            suffix = '分钟前';
            console.log(digit+suffix);
            if (digit >= 60) {
                digit = parseInt(digit / 60);
                suffix = '小时前';
                console.log(digit+suffix);
                if (digit >= 24) {
                    digit = parseInt(digit / 24);
                    suffix = '天前';
                    console.log(digit+suffix);
                    if (digit >= 30) {
                    digit = parseInt(digit / 30);
                    suffix = '个月前';
                    console.log(digit+suffix);
                        if (digit >= 12) {
                            digit = parseInt(digit / 12);
                            suffix = '年前';
                            console.log(digit+suffix);
                        }
                    }
                }
            }
        }
        return digit + suffix;
    }  
    // ctrl+H  替换
    handleTime("2021-07-02 19:41:01")
    </script>
</body>
</html>